home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12168 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: I need help.
  5. Date: 29 Mar 1996 09:52:30 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4jh0vu$13c@solutions.solon.com>
  8. References: <4j9hr7$skj@cronkite.seas.gwu.edu> <26MAR199615081545@erich.triumf.ca> <danpop.828046690@rscernix> <4jguak$n6e@news1.intercall.com>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4jguak$n6e@news1.intercall.com>,
  12. Steven Ovits <engevar@intercall.com> wrote:
  13. >"EOF which expands to an integral constant expression that is
  14. >returned by several functions to indicate 'end-of-file,' that is,
  15. >no more input from a stream."
  16.  
  17. >Notice it says nothing about whether the constant is valid in a file,
  18. >so it can be.
  19.  
  20. However, you miss two important points:
  21. 1.  All getchar type routines return the character *converted to unsigned
  22. char* and then converted to int.
  23. 2.  EOF is negative.
  24.  
  25. Unless sizeof(char) == sizeof(int), it is *impossible* for getchar()
  26. (or getc, or fgetc) to return EOF on a file that has not reached the
  27. end of file, i.e., it is impossible for getchar() to return EOF and
  28. feof() to return 0.
  29.  
  30. >Therefore, it is a good idea to always use feof and only feof to
  31. >test for the end of input.
  32.  
  33. This is untrue.  If you're willing to give up support for machines where
  34. char is the same size as int, you can use EOF safely.  Even on those
  35. machines, it may be safe to use EOF, if no value returned by getchar()
  36. from a file read will ever compare equal to EOF, which is (AFAICT)
  37. permissible.
  38.  
  39. In any event, even on such machines, testing for EOF, and then testing for
  40. feof(), will allow you to avoid the call to feof() for the vast majority
  41. of characters.  This is a good deal.
  42.  
  43. -s
  44. -- 
  45. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  46. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  47. FUCK the communications decency act.  Goddamned government.  [literally.]
  48. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  49.